#include "client.h"
#include "ui_client.h"
int Client::times = 0;
int Client::m_recvRightMsg = 0;
int Client::m_recvErrorMsg = 0;
int Client::m_sendRightMsg = 0;
int Client::m_sendErrorMsg = 0;

Client::Client(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Client)
{
    m_loadIp = "";
    m_timeRequest = "time request!";
    m_ip = "";

    m_timer = new QTimer(this);

    ui->setupUi(this);
    ui->spinBox_Num->setRange(1, 100);
    ui->spinBox_Time->setRange(1, 10);

    m_model = new QStandardItemModel();
    m_model->setHorizontalHeaderItem(0, new QStandardItem(QObject::tr("消息类型")));
    m_model->setHorizontalHeaderItem(1, new QStandardItem(QObject::tr("消息内容")));
    m_model->setHorizontalHeaderItem(2, new QStandardItem(QObject::tr("收发日期")));
    m_model->setHorizontalHeaderItem(3, new QStandardItem(QObject::tr("src_id")));
    //利用setModel()方法将数据模型与QTableView绑定
    ui->tableViewMsg->setModel(m_model);

    connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(startSending()));
    connect(m_timer, SIGNAL(timeout()), this, SLOT(sendMsg()));

    connect(&m_updSocket, SIGNAL(recvData()), this, SLOT(recvMsg()));//接收客户端发送的UDP数据消息
}

Client::~Client()
{
    delete ui;
}
/*
 *isSend为0的时候是接收初始化，为1的时候是发送初始化
 */
void Client::init(int isSend)
{
    m_timeRequest = "time request";
    m_loadIp = "";

    initSendMsg();
    QByteArray tmp = m_sendMsgContent.toLatin1();
    char *timeStd = tmp.data();
    for(int i = 0; i < m_sendMsgContent.length(); ++i)
    {
        m_msg.data[i] = timeStd[i];
    }
    m_msg.data[m_sendMsgContent.length()] = '\0';
    m_updSocket.initSocket(m_port);
}

void Client::initSendMsg()
{
    m_port = ui->lineEditPort_Send->text().toInt();
    m_msg.usr_id = ui->lineEdit_UsrID->text().toInt();
    m_msg.src_id = ui->lineEdit_SrcID->text().toInt();
    m_msg.dst_id = ui->lineEdit_DetID->text().toInt();
    m_msg.msg_type = 0;
    m_sendMsgContent = ui->lineEdit_SendMsg->text();
}

void Client::startSending()
{
    ui->pushButton->setEnabled(false);
    int timeOut = ui->spinBox_Time->text().toInt()*1000;
    m_timer->start(timeOut);
}

void Client::sendMsg()
{
    ui->tableViewMsg->setModel(m_model);
    init(1);
    QStringList msg = m_updSocket.parseMsg(m_msg);
    initSendMsg();
    m_updSocket.sendMsg(msg, QString::fromStdString(m_loadIp), m_port);

    ++times;
    if(times >= ui->spinBox_Num->text().toInt())
    {
        m_timer->stop();
        ui->pushButton->setEnabled(true);
        times = 0;
    }
    addSendMsgToTable();
    ++m_sendRightMsg;
    ui->lineEdit_SendRightMsgNum->setText(QString("%1").arg(m_sendRightMsg));
    ui->lineEdit_SendMsgSum->setText(QString("%1").arg(m_sendRightMsg + m_sendErrorMsg));
}

void Client::recvMsg()
{
    QStringList recvMsg = QString(m_updSocket.getRecvMsg()).split("$");
    cout << recvMsg.at(1).toStdString() << endl;
    if(!(recvMsg.at(1) == ui->lineEdit_UsrID->text()))
        return;
    QStringList displayInfo;
    /*qServerMsg << m_id << m_recvData.at(2) << m_id << "1" << ui->lineEdit_Msg->text();
    qServerMsg << m_loaderID << serverID;
    for(int i = 2; i < qClientMsg.length(); ++i)
    {
        qServerMsg << qClientMsg.at(i);
    }///*/

    displayInfo << "1" << recvMsg.at(4) << "recv" << recvMsg.at(0);
    fillMsg(displayInfo);

    ++m_recvRightMsg;
    ui->lineEdit_RecvRightMsgNum->setText(QString("%1").arg(m_recvRightMsg));
    ui->lineEdit_RecvMsgSum->setText(QString("%1").arg(m_recvRightMsg + m_recvErrorMsg));

}

void Client::addSendMsgToTable()
{
    char *content = new char[32];
    int i(0);
    for(i = 0; m_msg.data[i] != '\0'; ++i)
    {
        content[i] = m_msg.data[i];
    }
    content[i] = '\0';

    QString msgContent = QString(QLatin1String(content));
    QString msgTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");// + QTime::currentTime().toString("hh:mm:ss");
    QStringList msg;
    msg << QString("%1").arg(m_msg.msg_type) << msgContent << msgTime << QString("%1").arg(m_msg.src_id) ;
    fillMsg(msg);
}

void Client::addRecvMsgToTable()
{

}

void Client::fillMsg(QStringList msg)
{
    QString msgType = "未知";
    QString msgContent = msg.at(1);
    QString msgTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    QString src_id = msg.at(3);
    switch(msg.at(0).toInt())
    {
    case 0:
        msgType = "时间请求";
        break;
    case 1:
        msgType = "时间答应";
        break;
    case 2:
        msgType = "心跳请求";
        break;
    case 3:
        msgType = "心跳应答";
        break;
    }
    int rowCount = m_model->rowCount();
    m_model->setItem(rowCount, 0, new QStandardItem(msgType));
    m_model->setItem(rowCount, 1, new QStandardItem(msgContent));
    m_model->setItem(rowCount, 2, new QStandardItem(msgTime));
    m_model->setItem(rowCount, 3, new QStandardItem(src_id));
}
void Client::displayMsg()
{

}
